app.controller('GetUnitCtrl',function ($scope,$http,$location,$window,$state,$localStorage) {
	$scope.namedata = $localStorage.cur_user.uname;
	$scope.mmData = $localStorage.layers_info;
	$scope.b = {};	
	var initdata = [];
	for(var key in $scope.mmData){
		if(key=="所有学校"){
			initdata[0]="所有学校"
		}
		initdata.push(key);
	}	
	$scope.b.school_name = initdata[0];	
	$scope.search = function(){		
		var school_id,building_type,campus_building_id,building_name,layer_id,layer_name;
		if($scope.b.school_name!="所有学校"){			
			if($scope.b.buildingtype!=null&&$scope.b.buildingtype!=undefined&&$scope.b.buildingtype!=""){
				if($scope.b.building_name!=null&&$scope.b.building_name!=undefined&&$scope.b.building_name!=""){
					if($scope.b.layer_name!=null&&$scope.b.layer_name!=undefined&&$scope.b.layer_name!=""){
						school_id = $scope.mmData[$scope.b.school_name].school_id;
						building_type = $scope.b.buildingtype.split('+')[0];
						campus_building_id = $scope.b.layer_name.split('+')[1];
						building_name = $scope.b.building_name;
						layer_name = $scope.b.layer_name.split('+')[0];
						layer_id = $scope.b.layer_name.split('+')[2];						
					}else{
						school_id = $scope.mmData[$scope.b.school_name].school_id;
						building_type = $scope.b.buildingtype.split('+')[0];
						campus_building_id = $scope.mmData[$scope.b.school_name][$scope.b.buildingtype.split('+')[0]][$scope.b.building_name].campus_id;
						building_name = $scope.b.building_name;
						$scope.b.layer_name = "";
						layer_name = "所有层";
					}


				}else{

					school_id = $scope.mmData[$scope.b.school_name].school_id;
					building_type = $scope.b.buildingtype.split('+')[0];
					$scope.b.building_name = "";
					building_name = "所有楼";
				}

			}else{
				$scope.b.building_type = "";
				building_type = "所有类型";
				school_id = $scope.mmData[$scope.b.school_name].school_id;
			}
			
			
		}else{
			school_id = "";
			building_type = "";
			campus_building_id = "";
			building_name = "";
			layer_id = "" ;
			layer_name = "" ;
		}
		$scope.currentPage = 1;//初始当前页
        $scope.maxSize = 3;//最多显示3页其他的用···代替            
		$http.post(
            $scope.app.backendurl+"tbl-room/view-all",
            {    
                'page':$scope.currentPage,  
                'school_name':$scope.b.school_name,	
                'school_id':school_id,
                'building_type':building_type,
                'campus_building_id':campus_building_id,
                'building_name':building_name,                
                'layer_id':layer_id,
                'layer_name':layer_name,
                'token':$localStorage.token                  

            }
          ).success(function (data) {
            //console.log(data);
            if(data.code==0&&data.msg=="读取成功"){             
              $scope.aaData = data.data.list;
              $scope.totalItems = data.data.page.totalCount;          
            }
            if(data.code==20001&&data.msg=='请先登录'){
              $location.path('access.signin');
            }  

        $scope.pageChanged = function(){
        	//console.log($scope.currentPage)
        	 $http.post(
	            $scope.app.backendurl+"tbl-room/view-all",
	            {    
	                'page':$scope.currentPage,  
                	'school_name':$scope.b.school_name,	
                	'school_id':school_id,
                	'building_type':building_type,
                	'campus_building_id':campus_building_id,
               		'building_name':building_name,                
                	'layer_id':layer_id,
                	'layer_name':layer_name,
                	'token':$localStorage.token             

	            }
	          ).success(function (data) {
	          	//console.log(data);
	          	if(data.code==0&&data.msg=="读取成功"){             
	              $scope.aaData = data.data.list;	                    
	            }
	            if(data.code==20001&&data.msg=='请先登录'){
	              $location.path('access.signin');
	            }  
	          }).error(function(data){

        	 })
        }    
        }).error(function(data){
          console.log(data);
        })
	}


	$scope.search();
	// $scope.aaData = [{
	// 	unit_id:1,
	// 	name:'1舍405',
	// 	schoolname:'统计附中',
	// 	type:'宿舍楼',
	// 	buildingname:'1舍',
	// 	roomp:'曹婷',
	// 	roomguanliy:'张三',
	// 	weibao:'赵瑞'
	// },{
	// 	unit_id:2,
	// 	name:'2舍405',
	// 	schoolname:'统计附中',
	// 	type:'宿舍楼',
	// 	buildingname:'2舍',
	// 	roomp:'李四',
	// 	roomguanliy:'赵武',
	// 	weibao:'李林'
	// }];
	// $scope.getcampuslist = function(pa){
	// 	$scope.currentPage = 1;//初始当前页
	//     $scope.maxSize = 3;//最多显示3页其他的用···代替
	// 	$http.post(
	//         $scope.app.backendurl+"tbl-school/view-all",
	//         {  
	//         	'page': $scope.currentPage,       
	//             'token':$localStorage.token                  

	//         }
	//     ).success(function (data) {
	//       //console.log(data);
	//       if(data.code==0&&data.msg=="OK"){
	//       	$scope.aaData = data.data.list;
	//       	$scope.totalItems = data.data.page.totalCount;
	//       	setPage(pa);
	//       }
	//       if(data.code==20001&&data.msg=="请先登录"){
	//       	$location.path('access.signin');
	//       }
	      
	     
	//       $scope.pageChanged = function(){
	//       	//console.log($scope.currentPage);
	//       	$http.post(
	// 	        $scope.app.backendurl+"tbl-school/view-all",
	// 	        {
	// 	        	'page': $scope.currentPage,   
	// 	            'token':$localStorage.token                  

	// 	        }
	// 	    ).success(function (data) {
	// 	    	//console.log(data);
	// 	    	if(data.code==0&&data.msg=='OK'){
	// 		      	$scope.aaData = data.data.list;		      	
	// 		    }
	// 		    if(data.code==20001&&data.msg=="请先登录"){
	// 		      	$location.path('access.signin');
	// 		    }

	// 	     }).error(function (data) {
	// 		       alert(data);
	// 		 })
	//       }
	      
	    
	//     }).error(function (data) {
	//         alert(data);
	//     })
	// }
	// //console.log($state.params.page);
	// $scope.getcampuslist($state.params.page);
	


 //    function setPage (pageNo) {
	// 	$scope.currentPage = pageNo || '1';
	// 	$http.post(
	// 	        $scope.app.backendurl+"tbl-school/view-all",
	// 	        {
	// 	        	'page': $scope.currentPage,   
	// 	            'token':$localStorage.token                  

	// 	        }
	// 	    ).success(function (data) {

	// 	    	if(data.code==0&&data.msg=='OK'){
	// 		      	$scope.aaData = data.data.list;		      	
	// 		    }
	// 		    if(data.code==20001&&data.msg=="请先登录"){
	// 		      	$location.path('access.signin');
	// 		    }

	// 	     }).error(function (data) {
	// 		       alert(data);
	// 		 })	    
		
		
	// }

	
})



